package com.fxkj.cloud.drms.ui.listener;

import com.fxkj.cloud.dbs.common.constants.SyncStatus;
import com.fxkj.cloud.dbs.common.constants.WsMsgEnum;
import com.fxkj.cloud.dbs.common.event.stream.RemoteControlPlanSyncStatusEvent;
import com.fxkj.cloud.dbs.common.model.ws.ControlPlanSyncStatus;
import com.fxkj.cloud.dbs.common.model.ws.FxWsMsg;
import com.fxkj.cloud.drms.domain.ws.service.FxWsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.UUID;
import java.util.function.Consumer;

/**
 * <p>description: 处理远程控制指令下发事件</p>
 * @author ww
 * @date 2025/3/21 16:16
 */
@Component
@Slf4j
public class RemoteControlPlanSyncStatusListener {

    private final FxWsClient wsClient;

    public RemoteControlPlanSyncStatusListener(FxWsClient wsClient) {
        this.wsClient = wsClient;
    }

    @Bean
    public Consumer<RemoteControlPlanSyncStatusEvent> controlPlanSyncCallback() {
        return event -> {
            log.trace("收到控制计划下发回应:{}", event);
            UUID controlPlanId = event.getControlPlanId();
            UUID controlPlanDeviceId = event.getControlPlanDeviceId();
            SyncStatus status = event.getStatus();
            UUID organizationId = event.getOrganizationId();
            FxWsMsg<ControlPlanSyncStatus> controlPlanSyncStatusFxWsMsg = new FxWsMsg<>(WsMsgEnum.CONTROL_PLAN_DEVICE_STATUS, ControlPlanSyncStatus.of(controlPlanId, Collections.singleton(controlPlanDeviceId), status));
            //推送消息
            wsClient.pushNotificationToOrganization(organizationId, controlPlanSyncStatusFxWsMsg);
        };
    }

}
